//
//  TimeUtil.swift
//  Extend
//
//  Created by 郑浩 on 2017/9/28.
//  Copyright © 2017年 敬信. All rights reserved.
//

import UIKit

public class TimeUtil: NSObject {
    
    /// 获取当前时间的时间戳
    ///
    /// - Returns: 当前时间戳
    public class func getNowTimestamp () -> Int {
        //获取当前时间
        let now = Date()
        
        //创建一个日期格式器
        let dformatter = DateFormatter()
        dformatter.dateFormat = "yyyy年MM月dd日 HH:mm:ss"
        
        //当前时间的时间戳
        let timeInterval:TimeInterval = now.timeIntervalSince1970
        let timeStamp = Int(timeInterval)
        return timeStamp
    }
    

    /// 时间戳转时间
    ///
    /// - Parameters:
    ///   - stamp: 时间戳
    ///   - formatterStr: 时间格式字符串
    /// - Returns: 时间
    public class func stampToTime (stamp: Int, formatterStr: String) -> String {
        var currentStamp = stamp
        
        let str = "\(stamp)"
        if str.count == 13 {
            currentStamp = stamp/1000
        }
        
        let timeInterval: TimeInterval = TimeInterval(currentStamp)
        let date = Date(timeIntervalSince1970: timeInterval)
        
        let formatter = DateFormatter()
        formatter.dateFormat = formatterStr
        
        return formatter.string(from: date)
    }
    
    
    /// NSDate转时间
    ///
    /// - Parameters:
    ///   - stamp: date
    ///   - formatter: 时间格式字符串
    /// - Returns: 时间
    public class func stampToTime(date: NSDate, formatter: String) -> String {
        let format = DateFormatter.init()
        format.dateFormat = formatter
        let dateStr = format.string(from: date as Date)
        return dateStr
    }
    
    /// 两个时间戳相差的秒数
    ///
    /// - Parameters:
    ///   - start: 开始时间戳
    ///   - end: 结束时间戳
    /// - Returns: 秒数
    public class func getStampSeconds (start: Int, end: Int) -> Int {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        
        let startDate = Date(timeIntervalSince1970: TimeInterval(start))
        let endDate = Date(timeIntervalSince1970: TimeInterval(end))
        
        return Int(endDate.timeIntervalSince(startDate))
    }
    
    /// 两个时间之间差值（秒）
    ///
    /// - Parameters:
    ///   - end_time: 结束时间
    ///   - start_time: 开始时间
    /// - Returns: 差值
    public class func getTimeSeconds (end_time: String, start_time: String) -> Int {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        let seconds = Int(dateFormatter.date(from: end_time)!.timeIntervalSince1970 - dateFormatter.date(from: start_time)!.timeIntervalSince1970)
        return seconds
    }
    
    
    /// 两个时间差转时分秒
    ///
    /// - Parameters:
    ///   - end_time: 结束时间
    ///   - start_time: 开始时间
    ///   - method: 方法名
    /// - Returns: 时间差（分时秒）
    public class func getInterval (end_time: String, start_time: String, method: (_ aSeconds: Int) -> String) -> String {
        let seconds = getTimeSeconds(end_time: end_time, start_time: start_time)
        return method(seconds)
    }
    
    
    /// 秒转时：分：秒
    ///
    /// - Parameter seconds: 秒数
    /// - Returns: 时：分：秒
    func getHourStr (seconds: Int) -> String {
        let str_hour = "\(seconds / 3600)"
        let str_minute = "\(seconds % 3600 / 60)"
        let str_second = "\(seconds % 60)"
        let format_time = "\(str_hour):\(str_minute):\(str_second)"
        return format_time
    }
    
    
    /// 秒转分：秒
    ///
    /// - Parameter seconds: 秒数
    /// - Returns: 分：秒
    public class func getMinStr (seconds: Int) -> (minuteStr: String, secondStr: String) {
        let minute = seconds/60
        let minureStr = String(format:"%02d",minute)
        //format of second
        
        let second = seconds%60
        let secondStr = String(format:"%02d",second)
        //format of time
        return (minureStr, secondStr)
    }
    
    /// 是否是同一个月
    ///
    /// - Parameters:
    ///   - date1: 日期
    ///   - date2: 日期
    /// - Returns: 是否是同一个月
    public class func isSameMonth(_ date1: Date, _ date2: Date) -> Bool {
        let calendar = Calendar.current
        let comp1 = calendar.dateComponents([.year,.month], from: date1)
        let comp2 = calendar.dateComponents([.year,.month], from: date2)
        if comp1.year == comp2.year && comp1.month == comp2.month {
            return true
        } else {
            return false
        }
    }
    
    /// 是否是同一个月
    ///
    /// - Parameters:
    ///   - date1: 日期
    ///   - date2: 日期
    /// - Returns: 是否是同一个月
    public class func isSameYear(_ date1: Date, _ date2: Date) -> Bool {
        let calendar = Calendar.current
        let comp1 = calendar.dateComponents([.year], from: date1)
        let comp2 = calendar.dateComponents([.year], from: date2)
        if comp1.year == comp2.year {
            return true
        } else {
            return false
        }
    }
    
    
    /// 时间戳创建Date
    ///
    /// - Parameter timeStamp:时间戳
    /// - Returns:Date
    public class func createDateWithTimeStampString(_ timeStamp: String) -> Date {
        let interval: TimeInterval = (Double(timeStamp)!) * 0.001
        let date = Date.init(timeIntervalSince1970: interval)
        return date
    }
    
    public class func handleShowTime(timeStr: String?) -> String? {
        if timeStr != nil {
            let date1 = Date()
            let date2 = TimeUtil.createDateWithTimeStampString(timeStr!)
            let isSame = TimeUtil.isSameYear(date1, date2)
            var format = "yyyy-MM-dd HH:mm"
            if isSame {
                format = "MM-dd HH:mm"
            }
            let str = TimeUtil.stampToTime(stamp: Int(timeStr!)!, formatterStr:format)
            return str
        }
        return nil
    }
    
    
    
}
